package com.bangdream.michelia.view.activity.currency;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.NoScrollViewPager;
import com.bangdream.michelia.tool.StatusBarTool;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.view.activity.base.BaseActivity;
import com.bangdream.michelia.view.fragment.main.SlideRightFragment;
import com.bangdream.michelia.view.fragment.main.home.MainHome;
import com.bangdream.michelia.view.fragment.main.live.MainLive;
import com.bangdream.michelia.view.fragment.main.my.MyFragment;
import com.bangdream.michelia.view.fragment.main.curriculum.MainCurriculum;
import com.bangdream.michelia.view.fragment.main.task.MainTask;
import com.jaeger.library.StatusBarUtil;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener,OnMainActivityOperationListener{
    private NoScrollViewPager viewPager;
    private static final int itemNumb = 5;
    private FragmentPagerAdapter mAdapter;
    private RelativeLayout[]  btnItems = new RelativeLayout[itemNumb];
    private ImageView[] ivMenus = new ImageView[itemNumb];
    private TextView[] tvMenus = new TextView[itemNumb];
    // 界面底部的选中菜单按钮资源
    private int[] select_on = { R.mipmap.menu_1_b,R.mipmap.menu_2_b, R.mipmap.menu_3_b,R.mipmap.menu_4_b,R.mipmap.menu_5_b};
    // 界面底部的未选中菜单按钮资源
    private int[] select_off = { R.mipmap.menu_1_a,R.mipmap.menu_2_a, R.mipmap.menu_3_a,R.mipmap.menu_4_a,R.mipmap.menu_5_a};
    //底部textview文字控件
    private TextView[] tvs = new TextView[5];
    //底部文字textview的id
    private int[] tvs_id = {R.id.text1,R.id.text2,R.id.text3,R.id.text4,R.id.text5};
    //如果系统版本大于5.0 为ViewPage中页面最上面的布局设置高度等于状态栏高度 如果小于5.0则设置状态栏颜色为主题颜色 页面最上面的布局高度为0
    public static int statusBarHeight = 0;
    private MainHome itemHome;
    private MainCurriculum itemCurriculum;
    private MainTask itemTask;
    private MainLive itemLive;
    private MyFragment itemMy;
    //屏幕的宽高
    public static int screenWidth,screenHeight;
    //侧边栏控件
    private DrawerLayout drawerLayout;
    //存放侧边栏布局的容器
    private RelativeLayout main_right_drawer_layout;
    SlideRightFragment slideRightFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayout() {
        return R.layout.main_layout;
    }

    @Override
    public void findId() {
        drawerLayout = findViewById(R.id.main_drawer_layout);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        btnItems[0] =  findViewById(R.id.btnItem1);
        btnItems[1] =  findViewById(R.id.btnItem2);
        btnItems[2] =  findViewById(R.id.btnItem3);
        btnItems[3] =  findViewById(R.id.btnItem4);
        btnItems[4] =  findViewById(R.id.btnItem5);
        ivMenus[0] =  findViewById(R.id.iv_menu1);
        ivMenus[1] =  findViewById(R.id.iv_menu2);
        ivMenus[2] = findViewById(R.id.iv_menu3);
        ivMenus[3] =  findViewById(R.id.iv_menu4);
        ivMenus[4] =  findViewById(R.id.iv_menu5);
        tvMenus[0] =  findViewById(R.id.text1);
        tvMenus[1] =  findViewById(R.id.text2);
        tvMenus[2] =  findViewById(R.id.text3);
        tvMenus[3] =  findViewById(R.id.text4);
        tvMenus[4] =  findViewById(R.id.text5);
        //初始化底部按钮文字
        for (int i = 0; i < tvs.length; i++) {
            tvs[i] = findViewById(tvs_id[i]);
        }
        viewPager = findViewById(R.id.viewPage);
        viewPager.setNoScroll(false);
        itemHome = new MainHome();
        itemHome.setOnMainActivityOperationListener(this);
        itemCurriculum = new MainCurriculum();
        itemCurriculum.setOnMainActivityOperationListener(this);
        itemTask = new MainTask();
        itemLive = new MainLive();
        itemMy = new MyFragment();
        setRightSliding();
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                if(slideRightFragment!=null){
                    slideRightFragment.setData();
                }
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });


    }
    //设置右边侧边栏的布局
    private void setRightSliding(){
        //右边菜单
        main_right_drawer_layout = (RelativeLayout) findViewById(R.id.main_right_drawer_layout);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        slideRightFragment = new SlideRightFragment();
        slideRightFragment.setOnMainActivityOperationListener(this);
        ft.add(R.id.main_right_drawer_layout, slideRightFragment);
        ft.commit();
    }
    @Override
    public void setListener() {
        viewPager.addOnPageChangeListener(this);
        for (int i = 0;i<btnItems.length;i++)
        {
            btnItems[i].setOnClickListener(this);
        }
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        Intent intent2 = getIntent();
        int id = intent2.getIntExtra("flagIndex", 0);
        if (id > 0) {
            viewPager.setCurrentItem(id);
        }
    }

    @Override
    public void logic() {
        /**
         * 设置全屏并且显示状态栏
         */
        //5.0以上系统才支持这个效果 app内容占据导航栏 并且导航栏透明
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }else {
            StatusBarUtil.setColorNoTranslucent(this, ContextCompat.getColor(this,R.color.text_on));
        }

        //状态栏高度
        statusBarHeight = StatusBarTool.getStatusHeight(this);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        //设置键盘弹出样式 不挤压屏幕
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        screenWidth = dm.widthPixels;//宽度
        screenHeight = dm.heightPixels ;//高度
        L.d("宽度"+screenWidth);
        L.d("高度"+screenWidth);
        MainApplication.getInstance().setHeight(screenHeight);
        MainApplication.getInstance().setWidth(screenWidth);
        /**
         * 初始化Adapter
         */
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return  itemNumb;
            }

            @Override
            public Fragment getItem(int arg0) {
                switch (arg0)
                {
                    case 0:
                        return itemHome;
                    case 1:
                        return itemCurriculum;
                    case 2:
                        return itemTask;
                    case 3:
                        return itemLive;
                    case 4:
                        return itemMy;
                    default:
                        return itemMy;
                }
            }
        };

        viewPager.setOffscreenPageLimit(itemNumb);                              //四个页面都缓存 不用每次划开重新加载
        //viewPager.setOffscreenPageLimit(1);                              //四个页面都缓存 不用每次划开重新加载
        viewPager.setAdapter(mAdapter);
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==1){
            //允许侧边滑动
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        }else {
            //锁定侧边滑动
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }

        for(int i = 0;i<ivMenus.length;i++)
        {
            if(position==i)
            {
                ivMenus[position].setImageResource(select_on[position]);
                tvs[i].setTextColor(ContextCompat.getColor(this,R.color.common_red));
            }else {
                ivMenus[i].setImageResource(select_off[i]);
                tvs[i].setTextColor(ContextCompat.getColor(this,R.color.text_color));
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btnItem1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.btnItem2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.btnItem3:
                viewPager.setCurrentItem(2);
                break;
            case R.id.btnItem4:
                viewPager.setCurrentItem(3);
                break;
            case R.id.btnItem5:
                viewPager.setCurrentItem(4);
                break;
            default:
                break;
        }
    }

    @Override
    public void onOpenSlide(boolean isOpen) {
        if(drawerLayout.isDrawerOpen(main_right_drawer_layout)){
            drawerLayout.closeDrawers();
        }else {
            drawerLayout.openDrawer(main_right_drawer_layout);
        }
    }

    @Override
    public void onSlideItemClick(int position, CoursesTypeBean bean) {
        if(itemCurriculum!=null){
            itemCurriculum.setItemSwitch(position,bean);
        }
    }

}
